import scrapy
import json
from sun.items import PlayersItem


class NbaSpider(scrapy.Spider):
    name = 'nba'
    allowed_domains = ['nba.com']
    start_urls = ['https://china.nba.com/static/data/league/playerlist.json']

    def parse(self, response):
        data = json.loads(response.text)['payload']['players']
        for i in range(len(data)):
            self.logger.info(data[i]['playerProfile']['displayName'])
            self.logger.info(data[i]['playerProfile']['displayNameEn'])
            self.logger.info(data[i]['playerProfile']['country'])
            self.logger.info(data[i]['playerProfile']['countryEn'])
            self.logger.info(data[i]['playerProfile']['draftYear'])
            self.logger.info(data[i]['playerProfile']['height'])
            self.logger.info(data[i]['playerProfile']['weight'])
            self.logger.info(data[i]['playerProfile']['jerseyNo'])
            self.logger.info(data[i]['playerProfile']['position'])
            self.logger.info(data[i]['teamProfile']['city'])
            self.logger.info(data[i]['teamProfile']['displayAbbr'])
            self.logger.info(data[i]['teamProfile']['division'])
            self.logger.info(data[i]['teamProfile']['nameEn'])
            item = PlayersItem()
            item['displayName'] = data[i]['playerProfile']['displayName']
            item['displayNameEn'] = data[i]['playerProfile']['displayNameEn']
            item['draftYear'] = data[i]['playerProfile']['draftYear']
            item['height'] = data[i]['playerProfile']['height']
            item['weight'] = data[i]['playerProfile']['weight']
            item['jerseyNo'] = data[i]['playerProfile']['jerseyNo']
            item['position'] = data[i]['playerProfile']['position']
            item['city'] = data[i]['teamProfile']['city']
            item['displayAbbr'] = data[i]['teamProfile']['displayAbbr']
            item['division'] = data[i]['teamProfile']['division']
            item['nameEn'] = data[i]['teamProfile']['nameEn']
            yield item
